//
//  main.m
//  OcDay6homework
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
#if 1
    //第一题
//    要求 :
//    1、创建1个包含5个字符串元素的数组;
//    2、用for循环倒序输出所有元素;
//    3、用for...in完成所有字符串元素的拼接(生成一个新的字符串),并输出拼接好的字符串。
    
    NSArray *array = @[@"sichangbao",@"laohei",@"laochen",@"laoyu",@"Gtx1080"];
//    for (NSInteger i = array.count - 1; i >=0 ; i--) {
//        NSLog(@"%@",[array objectAtIndex:i]);
//    }
    
    NSMutableString *str = [[NSMutableString alloc] init];
    for (NSString *strk in array) {
        [str appendString:strk];
    }
    NSLog(@"%@",str);
    
#endif
#if 1
//    要求:
//    1、创建1个包含5个person对象的数组(姓名用英文);
//    2、用for循环找到年龄最大的Person对象;
//    3、用for...in遍历数组,把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    Person *per1 = [Person personWithName:@"tony.stark" withAge:30];
    Person *per2 = [Person personWithName:@"Moresen" withAge:49];
    Person *per3 = [Person personWithName:@"steve" withAge:110];
    Person *per4 = [Person personWithName:@"hulk" withAge:45];
    Person *per5 = [Person personWithName:@"thor" withAge:270];
    
    NSArray *perArray = [NSArray arrayWithObjects:per1,per2,per3,per4,per5,nil];
    NSInteger maxAge = 0;
    NSInteger maxAgeAtIndex = 0;
    for (NSInteger i = 0;i < perArray.count;i++){
        if(maxAge < [[perArray objectAtIndex:i] age]){
            maxAge = [[perArray objectAtIndex:i] age];
            maxAgeAtIndex = i;
        }
    }
    NSLog(@"%@",[[perArray objectAtIndex:maxAgeAtIndex] name]);
    
    
    
    for(Person *per in perArray){
        if(per.age > 50){
            per.name = [per.name stringByAppendingString:@"-brother"];
        }
    }
    NSLog(@"%@",[[perArray objectAtIndex:0] name]);
    
#endif
    
#if 1
//    要求 :
//    1、使用第二题目中生成的数组;
//    2、使用sortUsingSelector: 分别按年龄,姓名,性别进排行序,输出排序结果;
//    3、使用NSSortDescriptor,按姓名进行排序。
    NSArray *returnArray = [perArray sortedArrayUsingSelector:@selector(compareWithName:)];
    NSLog(@"%@",returnArray);
    
    NSArray *returnArray2 = [perArray sortedArrayUsingSelector:@selector(compareWithAge:)];
    NSLog(@"%@",returnArray2);
    
    NSSortDescriptor *sortDescript = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    NSArray *returnArray3 = [perArray sortedArrayUsingDescriptors:@[sortDescript]];
    NSLog(@"%@",returnArray3);
    
    
#endif
    return 0;
}
